<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc'
	);
	$list=$this->get_list('agent_area',$parameter,$total);
	if($list){
		$open_duodai=$this->setting['open_duodai'];
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			$item['open_duodai']=$this->setting['open_duodai'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'address','title'=>'代理地区','type'=>'string'),
			array('name'=>'simid','title'=>'代理关系上级','type'=>'string'),
			array('name'=>'level_id','title'=>'区域代理等级','type'=>'radio','options'=>'levels'),
			array('name'=>'status','title'=>'区域代理状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'区域订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'区域订单总金额','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'simid','title'=>'设置代理关系上级','show'=>array('level_id'=>array('a2','a3'))),
			array('type'=>'modal','op'=>'setlevel','title'=>'设置等级'),
			//array('type'=>'modal','op'=>'add','title'=>'修改代理区域'),
			array('type'=>'href','op'=>'duodai','title'=>'设置代理模式','show'=>array('open_duodai'=>1)),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['is_duodai']=array('不是','是');
	$return['levels']=$this->model('model')->get_levels(true);
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('member')->check_agent($apply_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('agent_area',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核区域代理申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'agent_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='add'){
	//选择代理地区
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['area']){
			exi('请先选择代理地区~','error');
		}
		$area_code=$this->model()->get_area_code($post['area']);	
		$agent_id=$this->model('member')->up_agent($post['mid'],$area_code,$post);
		$this->model('member')->check_agent($agent_id,1);
		exi('修改区域代理成功！','','list');
	}
	$item=$this->get('agent_area',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$item['area']=$item['area']?json_decode($item['area'],true):array('city'=>'');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改区域代理',
		'edit'=>array(
			array('type'=>'pcd','name'=>'area','title'=>'选择代理地区'),
		),
	);
	exi($return);
}
if($_RQ['op']=='simid'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$update=array(
			'simid'=>intval($post['simid']),
		);
		pdo_update('jy_area_psi_agent_area',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),array('simid','id','level_id'));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'代理关系上级',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'代理关系上级','name'=>'simid','options'=>'areas'),
	);
	$return['htmls']=$htmls;
	if($item['level_id']=='a3'){
		$areas=pdo_getall('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>'a2'),array('id','mid','area'));	
	}elseif($item['level_id']=='a2'){
		$areas=pdo_getall('jy_area_psi_agent_area',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>'a1'),array('id','mid','area'));	
	}
	$return['areas']=array();
	foreach($areas as $av){
		$pcd=json_decode($av['area'],true);
		$title=$pcd['province'].' '.$pcd['city'].' '.$pcd['district'];
		$return['areas'][]=array(
			'id'=>$av['mid'],
			'title'=>$title,
		);
	}
	exi($return);
}
if($_RQ['op']=='setlevel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$area_code=$this->model()->get_area_code($post['area']);
		$this->update('agent_area',array('level_id'=>$post['level_id'],'area_code'=>$area_code,'area'=>ijson_encode($post['area'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}
	$item=$this->get('agent_area',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定区域代理不存在！','error');
	}
	$item['area']=json_decode($item['area'],true);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置代理商等级',
		'edit'=>array(
			array('name'=>'level_id','title'=>'区域代理等级','type'=>'radio','options'=>'levels'),
			array('type'=>'pcd','name'=>'area','level'=>1,'title'=>'选择代理地区','show'=>array('level_id'=>'a1')),
			array('type'=>'pcd','name'=>'area','level'=>2,'title'=>'选择代理地区','show'=>array('level_id'=>'a2')),
			array('type'=>'pcd','name'=>'area','level'=>3,'title'=>'选择代理地区','show'=>array('level_id'=>'a3')),
			array('type'=>'pcd','name'=>'area','level'=>4,'title'=>'选择代理地区','show'=>array('level_id'=>'a4')),
		),
	);
	$return['levels']=$this->model('model')->get_leveldemo();
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$mid=$this->getcolumn('agent_area',array('id'=>$id),'mid');
		$this->delete('agent_area',array('id'=>$id));
		$this->delete('member',array('mid'=>$mid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$mid=$this->getcolumn('agent_area',array('id'=>$id),'mid');
				$this->delete('agent_area',array('id'=>$id));
				$this->delete('member',array('mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>